<template>
  <div>
    <el-table
      :data="shopList"
      style="width: 90%"
      height="700"
      align="center"
      empty-text="当前没有数据"
    >
      <el-table-column label="基本信息" width="200" align="center">
        <template #default="scope" align="center">
          <div style="dis">
            <img
              style="width: 88px; height: 88px"
              :src="scope.row.ImgUrl"
              alt=""
            />
            <div style="margin-left: 10px">
              {{ scope.row.Name }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="250" align="center">
        <template #default="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span style="margin-left: 10px" v-if="scope.row.PriceType == 1"
            >现价：￥{{ scope.row.Price }}</span
          >
          <span style="margin-left: 10px" v-if="scope.row.PriceType == 2"
            >￥{{ scope.row.Price + '——' + '￥' + scope.row.Price2 }}
          </span>
          <span style="margin-left: 10px" v-if="scope.row.PriceType == 3"
            >{{
              '市场价：￥' +
              scope.row.Price +
              '—' +
              '现价：￥' +
              scope.row.Price2
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="链接" width="200" align="center">
        <template #default="scope">
          <!-- <i class="el-icon-time"></i> -->
          <span style="margin-left: 10px">{{ scope.row.Url }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300" align="center">
        <template #default="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="primary"
            @click="handleModify(scope.$index, scope.row)"
            >修改价格</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block-pagination">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        v-model:currentPage="currentPage"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="totalPage"
      >
      </el-pagination>
    </div>
    <ShopDialog :goodsId="goodsId" ref="ShopDialog"></ShopDialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, watch, inject } from 'vue'
import ShopDialog from './ShopDialog.vue'
import { goShopList, goShopDelete } from '@/service/main/main'
import { ElMessageBox, ElMessage } from 'element-plus'
export default defineComponent({
  props: ['searchInput'],
  components: { ShopDialog },
  async setup(props) {
    // 刷新页面
    const reload: any = inject('reload')
    let dialogVisible = ref(false)
    let ShopDialog = ref()
    let goodsId = ref()
    onMounted(() => {
      return ShopDialog
    })
    // eslint-disable-next-line vue/no-setup-props-destructure
    let searchInputProps = ''
    watch(
      () => props.searchInput,
      (newValue, oldValue) => {
        console.log({ newValue, oldValue })
        searchInputProps = newValue
        goList(newValue)
      },
    )
    // 获取商品列表
    let shopList = ref()
    const currentPage = ref(1)
    let shopRes = ref()
    // 向上取整页码书
    // 每页页数
    const pages = 5
    let totalPage = ref()
    const goList = async (searchInputProps: any) => {
      // 分页
      shopRes.value = await goShopList(
        searchInputProps,
        currentPage.value - 1,
        pages,
        2,
      )
      totalPage.value = Math.ceil(shopRes.value.Total / pages) * 100
      shopList.value = shopRes.value.List
    }
    goList(searchInputProps)

    const handleCurrentChange = async (val: any) => {
      currentPage.value = val
      let shopRes = await goShopList(
        searchInputProps,
        currentPage.value - 1,
        pages,
        2,
      )
      shopList.value = shopRes.List
      console.log(shopList.value)
    }
    const handleModify = ($index: any, row: any) => {
      // console.log($index, row)
      ShopDialog.value.dialogFormVisible = true
      goodsId.value = row.GoodsId
    }

    // 删除
    const open = (goodsId: number | string) => {
      ElMessageBox.confirm('此操作将永久删除直播间, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          goShopDelete(goodsId)
          dialogVisible.value = false
          ElMessage({
            type: 'success',
            message: '删除成功!',
          })
          // 刷新页面
          reload()
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '已取消删除',
          })
        })
    }
    const handleDelete = (row: any) => {
      open(row.goodsId)
    }

    return {
      shopList,
      shopRes,
      goList,
      dialogVisible,
      ShopDialog,
      goodsId,
      currentPage,
      totalPage,
      searchInputProps,
      handleDelete,
      handleModify,
      handleCurrentChange,
    }
  },
})
</script>

<style scoped>
.block-pagination {
  padding: 10px 20px;
}
</style>
